package com.ocom.mqsimple.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.MqSimpleApiUrl;
import com.ocom.common.entity.company.EntityComDockApi;
import com.ocom.common.enums.DockApiCodeEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.simple.ssgl.SSGLAttendanceRecordRequest;
import com.ocom.common.request.simple.ssgl.SSGLMqBaseRequest;
import com.ocom.common.request.simple.ssgl.SSGLNewPersonInfoRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mqsimple.business.ocomkq.impl.OcomKQProductServiceImpl;
import com.ocom.mqsimple.business.wgkq.impl.WGKQProductServiceImpl;
import com.ocom.mqsimple.feign.CompanyClientService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class OcomKQController {

	private final Logger logger = LoggerFactory.getLogger(OcomKQController.class);

	@Autowired
	private OcomKQProductServiceImpl productService;
	@Autowired
	private CompanyClientService companyClientService;

	@PostMapping(MqSimpleApiUrl.URL_ocomkqMqNewPersonInfo)
	public Result newPersonInfo(@RequestBody SSGLNewPersonInfoRequest request){
		try {
			log.info("OcomKQ=newPersonInfo=====参数：【{}】", request);
			SSGLMqBaseRequest baseRequest = new SSGLMqBaseRequest();

			String dockApiCode = DockApiCodeEnum.ocomkq_newPersonInfo.getValue();
			Result comDockApiResult = companyClientService.getComDockApi("ocomkq", request.getComId(),dockApiCode, 1);
			EntityComDockApi entityComDockApi = JSON.parseObject(JSON.toJSONString(comDockApiResult.getData()), EntityComDockApi.class);

			if (null != entityComDockApi && 0 < entityComDockApi.getAutoSize()) {
				request.setAutoSize(entityComDockApi.getAutoSize());
			}
			baseRequest.setMessage(JSON.toJSONString(request));
			baseRequest.setType("newPersonInfo");
			productService.handle(JSON.toJSONString(baseRequest));
		}catch (Exception e){
			logger.error("发送mq失败  错误【{}】",e);
			return ResultUtil.error(ResultEnum.Err_4006);
		}
		return ResultUtil.success();
	}

	@PostMapping(MqSimpleApiUrl.URL_ocomkqMqAttendanceRecord)
	public Result ocomKqAttendanceRecord(@RequestBody SSGLAttendanceRecordRequest request){
		try {
			log.info("OcomKQ=attendanceRecord=====参数：【{}】", request);
			SSGLMqBaseRequest baseRequest = new SSGLMqBaseRequest();

			String dockApiCode = DockApiCodeEnum.ocomkq_attendanceRecord.getValue();
			Result comDockApiResult = companyClientService.getComDockApi("ocomkq", request.getComId(),dockApiCode, 1);
			EntityComDockApi entityComDockApi = JSON.parseObject(JSON.toJSONString(comDockApiResult.getData()), EntityComDockApi.class);

			if (null != entityComDockApi && 0 < entityComDockApi.getAutoSize()) {
				request.setAutoSize(entityComDockApi.getAutoSize());
			}
			baseRequest.setMessage(JSON.toJSONString(request));
			baseRequest.setType("attendanceRecord");
			productService.handle(JSON.toJSONString(baseRequest));
		}catch (Exception e){
			logger.error("发送mq失败  错误【{}】",e);
			return ResultUtil.error(ResultEnum.Err_4006);
		}
		return ResultUtil.success();
	}

}
